//ignore: unused_import
import 'dart:convert';
import 'dart:ffi';

import 'package:shared_preferences/shared_preferences.dart';
import 'package:FiduMart/core/network/model/account_userInfo_entity.dart';
import 'package:FiduMart/core/network/model/login_entity.dart';

class PrefUtils {
  static SharedPreferences? _sharedPreferences;
  final keylang = "um_lang";
  final keyToken = "um_keyGoToken";//goclient token是否设置
  final keyLoginEntity = "um_login_entity";//登录信息
  final keyUserEntity = "um_user_entity";//用户信息
  final webCacheVersion = "webCacheVersion";//刷新web缓存刷新
  final lastEmail = "lastAccount";//上次登录的账号
  final lastPwd = "lastPassword";//上次登录的密码
  final auditIos = "auditIos";
  // PrefUtils() {
  //   SharedPreferences.getInstance().then((value) {
  //     _sharedPreferences = value;
  //   });
  // }

  Future<String?> getLang() async {
    return await _sharedPreferences?.getString(keylang);
  }

  Future<bool> setLang(String val) async {
    return await _sharedPreferences?.setString(keylang, val) ?? false;
  }


  Future<String> getToken() async {
    return await _sharedPreferences?.getString(keyToken) ?? '';
  }

  Future<void> setToken(String val) async {
    await _sharedPreferences?.setString(keyToken, val);
  }

  Future<LoginEntity> getLoginEntity() async {
    String? loginEntityJson = await _sharedPreferences?.getString(keyLoginEntity);
    if (loginEntityJson == null) {
      return LoginEntity();
    }
    return LoginEntity.fromJson(jsonDecode(loginEntityJson));
  }

  Future<void> setLoginEntity(LoginEntity loginEntity) async {
    String loginEntityJson = jsonEncode(loginEntity.toJson());
    await _sharedPreferences?.setString(keyLoginEntity, loginEntityJson);
  }

  Future<AccountUserInfoEntity> getUserEntity() async {
    String? userEntityJson = await _sharedPreferences?.getString(keyUserEntity);
    if (userEntityJson == null) {
      return AccountUserInfoEntity();
    }
    return AccountUserInfoEntity.fromJson(jsonDecode(userEntityJson));
  }

  Future<void> setUserEntity(AccountUserInfoEntity userEntity) async {
    String userEntityJson = jsonEncode(userEntity.toJson());
    await _sharedPreferences?.setString(keyUserEntity, userEntityJson);
  }

  Future<void> init() async {
    // SharedPreferences 存储在应用程序的私有文件夹中，其名称由系统自动分配，无法更改。
    _sharedPreferences ??= await SharedPreferences.getInstance();
  }

  ///will clear all the data stored in preference
  void clearPreferencesData() async {
    _sharedPreferences!.clear();
  }

  Future<int> getWebCacheVersion() async {
    return await _sharedPreferences?.getInt(webCacheVersion) ?? 0;
  }

  Future<void> setWebCacheVersion(int val) async {
    await _sharedPreferences?.setInt(webCacheVersion, val);
  }

  Future<String?> getLastEmail() async {
    return await _sharedPreferences?.getString(lastEmail)?? '';
  }

  Future<void> setLastEmail(String val) async {
    await _sharedPreferences?.setString(lastEmail, val);
  }

  Future<String?> getLastPwd() async {
    return await _sharedPreferences?.getString(lastPwd)?? '';
  }

  Future<void> setLastPwd(String val) async {
    await _sharedPreferences?.setString(lastPwd, val);
  }

  Future<String?> getAuditIos() async {
    return await _sharedPreferences?.getString(auditIos)?? '';
  }

  Future<void> setAuditIos(String val) async {
    await _sharedPreferences?.setString(auditIos, val);
  }

  Future<void> clearLoginEntity() async {
    await _sharedPreferences?.remove(keyLoginEntity);
  }
}
